package wedaex.content;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import wedaex.helper.HtmlProcessor;

@SuppressWarnings("serial")
public class SimpleGetContentSource implements ContentSource {
	private String url;
	private String proxyUrl;
	private int proxyPort;
	private String proxyUsername;
	private String proxyPassword;

	public static SimpleGetContentSource create(String url) {
		return new SimpleGetContentSource(url, null, 0, null, null);
	}
	
	public static SimpleGetContentSource createWithProxy(String url, String proxyUrl, int proxyPort, String proxyUsername, String proxyPassword) {
		return new SimpleGetContentSource(url, proxyUrl, proxyPort, proxyUsername, proxyPassword);
	}
	
	private SimpleGetContentSource(String url, String proxyUrl, int proxyPort, String proxyUsername, String proxyPassword) {
		this.url = url;
		this.proxyUrl = proxyUrl;
		this.proxyPort = proxyPort;
		this.proxyUsername = proxyUsername;
		this.proxyPassword = proxyPassword;
	}


	public Page retrieveContent() throws Exception {
		HttpClient client = createHttpClient();

		HttpMethod method = new GetMethod(url);
		int status = client.executeMethod(method);
		if(status!=200) throw new RetrieveContentException("Server return status: " + status);

		String encoding = HtmlProcessor.getEncoding(method.getResponseHeaders());
		String content = method.getResponseBodyAsString();
		return new Page(this, content, encoding);
	}

	private HttpClient createHttpClient() {
		HttpClient client = new HttpClient();
		if(proxyUrl!=null && proxyPort > 0) {
			client.getHostConfiguration().setProxy("slave.wisers.com", 33128);
			if(proxyUsername!=null && proxyPassword!=null)
				client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials("wisers", "wisers"));
		}
		return client;
	}

	public String getUrl() {
		return this.url;
	}
	
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
				.append("url", url)
				.append("isProxied", proxyUrl!=null && proxyPort>0)
				.toString();
	}

	public Element toXml(Document doc) {
		// source tag
		Element source = doc.createElement("source");
		source.setAttribute("type", "simpleGetSource");
		source.setAttribute("url", this.url);
		
		// proxy tag
		if(proxyUrl!=null) {
			Element proxy = doc.createElement("proxy");
			proxy.setAttribute("url", this.proxyUrl);
			proxy.setAttribute("port", this.proxyPort + "");
			proxy.setAttribute("username", this.proxyUsername);
			proxy.setAttribute("password", this.proxyPassword);
			source.appendChild(proxy);
		}
		return source;
	}
}
